package controllers

import (
	"beego/comm/db"
	"beego/comm/entity"
	"encoding/json"
	"fmt"

	"github.com/astaxie/beego"
)

type DelFoodController struct {
	beego.Controller
}

func (c *DelFoodController) DelFood() {
	defer func() {
		err := recover()
		if nil != err {
			c.Data["json"] = map[string]interface{}{
				"status":    false,
				"errorInfo": err,
			}
			c.ServeJSON()
		}
	}()
	var food_record entity.FoodRecordDel
	json.Unmarshal(c.Ctx.Input.RequestBody, &food_record)
	user_name := food_record.UserName
	user_loginID := food_record.LoginID
	loginID, err := db.RedisGet("login-" + user_name)
	if nil != err {
		panic(fmt.Sprintf("%s", err.Error()))
	}
	if "" == loginID || loginID != user_loginID {
		panic("请先登录")
	}
	for _, esid := range food_record.Record {
		db.EsDelete("food_record_index", "food_records", esid)
	}
	c.Data["json"] = map[string]interface{}{
		"status":    true,
		"errorInfo": "",
	}
	c.ServeJSON()
}
